package cn.lastcc.toolsCollection.vessel;

import javax.swing.JOptionPane;

/**
 * 对话框
 * @author baidongyang
 *
 */
public class MessageDialog {

	// 下一层按钮对应的常量：close -1; OK 0; yes 0; no 1; cancel 2;

	// TODO 本类中冗余的内容需要优化(重构)

	/*
	 * 防止被构造
	 */
	private MessageDialog() {}

	/**
	 * 无外观显示对话框
	 * @param content 显示的内容
	 */
    public static void show(Object content) {
    	JOptionPane.showMessageDialog(null, content);
    }

    /**
     * 显示带有标题和内容的无外观对话框
     * @param content 显示的内容
     * @param title 对话框标题
     */
    public static void show(Object content, String title) {
    	JOptionPane.showMessageDialog(null, content, title, DialogIconConst.NONE);
    }

    /**
     * 显示定制提示图标的对话框
     * @param content 显示的内容
     * @param title 对话框的标题
     * @param icon 要显示的提示图标类型
     */
    public static void show(Object content, String title, DialogIconType dialogIconType) {
    	switch(dialogIconType) {
    	case None:
    		JOptionPane.showMessageDialog(null, content, title, DialogIconConst.NONE);
    		break;
    	case Information:
    		JOptionPane.showMessageDialog(null, content, title, DialogIconConst.INFORMATION);
    		break;
    	case Question:
    		JOptionPane.showMessageDialog(null, content, title, DialogIconConst.QUESTION);
    		break;
    	case Warning:
    		JOptionPane.showMessageDialog(null, content, title, DialogIconConst.WARNING);
    		break;
    	case Error:
    		JOptionPane.showMessageDialog(null, content, title, DialogIconConst.ERROR);
    		break;
    	}
    }

    /**
     * 弹出可以自定义按钮的对话框
     * @param content 显示的内容
     * @param title 对话框的标题
     * @param buttonType 按钮的类型
     * @return 用户点击的按钮类型
     */
    public static DialogButtonResult show(Object content, String title, DialogButtonType dialogButtonType) {
    	int result = -1;
    	// 获取按钮对应的常量
    	switch(dialogButtonType) {
    	case OK:
    		JOptionPane.showMessageDialog(null, content, title, DialogButtonConst.OK);
    		return DialogButtonResult.Yes_or_OK;
    	case Yes_No:
    		result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.YES_NO, DialogIconConst.QUESTION);
    		break;
    	case OK_Cancel:
    		result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.OK_CANCEL, DialogIconConst.QUESTION);
    		break;
    	case Yes_No_Close:
    		result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.YES_NO_CANCEL, DialogIconConst.QUESTION);
    		break;
    	}

    	return getButtonResult(result);
    }

    /**
     * 弹出对话框<br>
     * 该对话框可以指定按钮的类型和左侧显示的提示图片类型
     * @param content 显示的内容
     * @param title 对话框的标题
     * @param buttonType 显示的按钮的类型
     * @param icon 显示的提示图标的类型
     * @return 用户点击的按钮类型
     */
    public static DialogButtonResult show(Object content, String title, DialogButtonType dialogButtonType, DialogIconType dialogIconType) {
    	int result = -1;
    	switch(dialogButtonType) {
    	case OK:
    		show(content, title, dialogIconType);
    		return DialogButtonResult.Yes_or_OK;
    	case Yes_No:
    		switch(dialogIconType) {
    		case None:
    			result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.YES_NO, DialogIconConst.NONE);
    			break;
        	case Information:
        		result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.YES_NO, DialogIconConst.INFORMATION);
        		break;
        	case Question:
        		result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.YES_NO, DialogIconConst.QUESTION);
        		break;
        	case Warning:
        		result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.YES_NO, DialogIconConst.WARNING);
        		break;
        	case Error:
        		result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.YES_NO, DialogIconConst.ERROR);
        		break;
    		}
    		break;
    	case OK_Cancel:
    		switch(dialogIconType) {
    		case None:
    			result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.OK_CANCEL, DialogIconConst.NONE);
    			break;
        	case Information:
        		result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.OK_CANCEL, DialogIconConst.INFORMATION);
        		break;
        	case Question:
        		result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.OK_CANCEL, DialogIconConst.QUESTION);
        		break;
        	case Warning:
        		result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.OK_CANCEL, DialogIconConst.WARNING);
        		break;
        	case Error:
        		result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.OK_CANCEL, DialogIconConst.ERROR);
        		break;
    		}
    		break;
    	case Yes_No_Close:
    		switch(dialogIconType) {
    		case None:
    			result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.YES_NO_CANCEL, DialogIconConst.NONE);
    			break;
        	case Information:
        		result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.YES_NO_CANCEL, DialogIconConst.INFORMATION);
        		break;
        	case Question:
        		result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.YES_NO_CANCEL, DialogIconConst.QUESTION);
        		break;
        	case Warning:
        		result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.YES_NO_CANCEL, DialogIconConst.WARNING);
        		break;
        	case Error:
        		result = JOptionPane.showConfirmDialog(null, content, title, DialogButtonConst.YES_NO_CANCEL, DialogIconConst.ERROR);
        		break;
    		}
    		break;
    	}

    	return getButtonResult(result);
    }

    /*
     * 获取用户选择的结果
     * @param result 用户点击的按钮常量类型
     * @result 返回对应的按钮枚举类型
     */
    protected static DialogButtonResult getButtonResult(int result) {
     	switch(result) {
     	case -1:
     		return DialogButtonResult.Close;
     	case 0:
     		return DialogButtonResult.Yes_or_OK;
     	case 1:
     		return DialogButtonResult.NO;
     	case 2:
     		return DialogButtonResult.Cancel;
 		default:
 			return DialogButtonResult.Close;
     	}
    }
}
